

import Game from "../game/Game";
import BulletVO from "../db/vo/BulletVO";
import BulletData from "../game/data/BulletData";
import MonsterShader from "../game/shader/HitShader";
import SoundType from "../sound/SoundType";
import SoundMgr from "../sound/SoundMgr";
import MonsterScript from "./MonsterScript";

export default class BulletScript extends Laya.Script3D {


    private _speed: number = 10.5;
    private _sp: Laya.Sprite3D;
    private _direction: Laya.Vector3;


    private _muzzle: Laya.ShuriKenParticle3D;

    private _projectile: Laya.ShuriKenParticle3D;

    private _rgbd: Laya.Rigidbody3D;

    private _isFire: boolean = false;

    protected _targetPos: Laya.Vector3 = new Laya.Vector3(0, 0, 0);

    private _outPutPos: Laya.Vector3 = new Laya.Vector3(0, 0, 0);

    private _fireDistance: number = 15;



    private _bulletData: BulletData;


    public onAwake() {

        this._projectile = this.owner as Laya.ShuriKenParticle3D;

    }

    public set bulletData(value) {
        this._bulletData = value;
    }


    public onUpdate() {
        if (this._isFire === true) {
            // if(this.sd>=400){

            //     (this._projectile.getChildAt(0) as Laya.TrailSprite3D).trailRenderer.enable=true;
            // }
            this._projectile.transform.position = this._targetPos;
            // (this._projectile.getChildAt(0) as Laya.TrailSprite3D).transform.position=this._targetPos;
            // this.sd+=8;
        }
    }


    public onTriggerEnter(other: Laya.PhysicsComponent): void {
        // console.log("bullet trigger enter: ", other,other.owner.name);
        if (other.owner.name === "monster") {
            // console.log("monster onTriggerEnter",other.owner)
            (other.owner.getComponent(MonsterScript) as MonsterScript).hit(this._bulletData);
            let hitParticle: Laya.ShuriKenParticle3D = Game.hero.bullet.getHit(this._bulletData.index);

            Game.layer3d.addChild(hitParticle)
            hitParticle.transform.position = this._projectile.transform.position;
            other.owner.parent.frameOnce(150, other.owner.parent, () => {
                Game.layer3d.removeChild(hitParticle)
            })
            this.stop();

            hitParticle.particleSystem.play();
            SoundMgr.Ins.playEffect(SoundType.EXPLOSION);
        }

    }


    public setDirection(fireTransform: Laya.Transform3D): void {
        this._projectile.transform.position = fireTransform.position;
        this._projectile.transform.position.cloneTo(this._targetPos);
        this._projectile.particleSystem.play();

        // this._projectile.transform.scale = new Laya.Vector3(3, 3, 3);
        fireTransform.worldMatrix.getForward(this._outPutPos);
        Laya.Vector3.normalize(this._outPutPos, this._outPutPos)
        this._outPutPos = new Laya.Vector3(this._outPutPos.x * this._fireDistance, this._outPutPos.y * this._fireDistance, this._outPutPos.z * this._fireDistance);

        this._projectile.transform.lookAt(new Laya.Vector3(-this._outPutPos.x, -this._outPutPos.y, -this._outPutPos.z), new Laya.Vector3(0, 1, 0));
        Laya.Tween.to(this._targetPos, this._outPutPos, this._speed * 300, null, Laya.Handler.create(this, this.stop));
        this._isFire = true;
    }

    public stop() {
        this._isFire = false;
        // console.log(this._projectile)
        if (this._projectile && this._projectile.particleSystem) {
            this._projectile.particleSystem.stop();
            this._projectile.transform.position = new Laya.Vector3(-1000, -1000, -1000);
        }
    }


    // onUpdate() {
    //     if(this._direction){
    //         this._sp.transform.translate(this._speed, false);
    //     }
    // }


}